<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <button>停止定时器</button>
  <script>
    // window.clearTimeout(timeId);
    let btn = document.querySelector("button");
    let setId = setTimeout(function () {
      console.log(`开启了定时了`);
    }, 5000);
    btn.addEventListener("click", function () {
      window.clearTimeout(setId);
    });
  </script>
</body>

</html>